﻿using System;
using log4net;

namespace Machinery.Cogs
{
    public interface ILogCog
    {
        /// <summary>
        /// Writes something to the log.
        /// </summary>
        /// <param name="loggerName">The logger name. For instance "Payment" </param>
        /// <param name="level">The logging level. LoggingLevel.Info, LoggingLevel.Warn,  LoggingLevel.Debug or LoggingLevel.Error</param>
        /// <param name="message">The message you want written in the log.</param>
        /// <param name="exception">Can be null. Exception with stack trace that you want to include in the log entry.</param>
        void Log(string loggerName, LoggingLevel level, string message, Exception exception);
    }

    public enum LoggingLevel
    {
        Info,
        Debug,
        Warn, 
        Error
    }

    /// <summary>
    /// The standard LogCog simply wraps log4net.
    /// </summary>
    public class LogCog : Cog, ILogCog
    {
        /// <summary>
        /// Writes something to the log via log4net. The log file can be found in Logs/NMachinery.txt
        /// </summary>
        /// <param name="loggerName">The logger name. If you set this to 'Payment' the Logger Name in the log4net file will be 'NMachinery.Payment'.</param>
        /// <param name="level">The logging level. LoggingLevel.Info, LoggingLevel.Warn,  LoggingLevel.Debug or LoggingLevel.Error</param>
        /// <param name="message">The message you want written in the log.</param>
        /// <param name="exception">Can be null. Exception with stack trace that you want to include in the log entry.</param>
        public void Log(string loggerName, LoggingLevel level, string message, Exception exception)
        {
            var logger = LogManager.GetLogger("NMachinery." + loggerName);
            switch (level)
            {
                case LoggingLevel.Info:
                    if (exception == null)
                        logger.Info(message); 
                    logger.Info(message, exception);
                    break;
                
                case LoggingLevel.Debug:
                    if (exception == null)
                        logger.Debug(message);
                    logger.Debug(message, exception);
                    break;

                case LoggingLevel.Warn:
                    if (exception == null)
                        logger.Warn(message);
                    logger.Warn(message, exception);
                    break;

                case LoggingLevel.Error:
                    if (exception == null)
                        logger.Error(message);
                    logger.Error(message, exception);
                    break;
            }
            
        }


    }
}
